package com.wkbb.basic.thread;

import com.wkbb.basic.feign.DoorFeignService;
import com.wkbb.basic.service.XmppService;
import com.wkbb.common.enums.RoleTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;

import java.util.Date;

/**
 * @author yangdaofang
 * 创建时间：2019-11-12 11:13
 */
@Slf4j
public class SynUserAccessXmppTask implements Runnable{
    private DoorFeignService doorFeignService;
    private Long orgId;
    private Long userId;
    private RoleTypeEnum roleTypeEnum;
    private Integer isDel = 0;

    //xmpp推送更新学生家长关系
    @Lazy
    private XmppService xmppService;
    private Long id;
    private Long studentId;
    private Integer relationType;
    private Boolean userFlag;
    private Boolean studentFlag;
    private Boolean userToDoorFlag;
    private Boolean flag;
    private Short roleType;
    private Date date;

    public SynUserAccessXmppTask(DoorFeignService doorFeignService, Long orgId, Long userId, RoleTypeEnum roleTypeEnum,XmppService xmppService,Long id,Long studentId,Integer relationType,Boolean userFlag,Boolean studentFlag, Boolean userToDoorFlag,  Boolean flag,Short roleType,Date date) {
        this.doorFeignService = doorFeignService;
        this.orgId = orgId;
        this.userId = userId;
        this.roleTypeEnum = roleTypeEnum;
        this.xmppService = xmppService;
        this.id = id;
        this.studentId = studentId;
        this.relationType = relationType;
        this.userFlag = userFlag;
        this.studentFlag = studentFlag;
        this.userToDoorFlag = userToDoorFlag;
        this.flag = flag;
        this.roleType = roleType;
        this.date = date;
    }

    public SynUserAccessXmppTask(DoorFeignService doorFeignService, Long orgId, Long userId, RoleTypeEnum roleTypeEnum, Integer isDel,XmppService xmppService,Long id,Long studentId,Integer relationType,Boolean userFlag,Boolean studentFlag, Boolean userToDoorFlag, Boolean flag,Short roleType,Date date) {
        this.doorFeignService = doorFeignService;
        this.orgId = orgId;
        this.userId = userId;
        this.roleTypeEnum = roleTypeEnum;
        this.isDel = isDel;
        this.xmppService = xmppService;
        this.id = id;
        this.studentId = studentId;
        this.relationType = relationType;
        this.userFlag = userFlag;
        this.studentFlag = studentFlag;
        this.userToDoorFlag = userToDoorFlag;
        this.flag = flag;
        this.roleType = roleType;
        this.date = date;
    }

    @Override
    public void run() {
        try {

            if(userFlag){
                log.info("xmpp推送用户信息.....start，orgId:{},userId:{}",orgId, userId);
                xmppService.xmppUpdatePerson(orgId, userId,roleType,date);
                log.info("xmpp推送用户信息完毕.....end，orgId:{},userId:{}",orgId, userId);
            }

            if(studentFlag){
                log.info("xmpp推送学生家长关系.....start，orgId:{},userId:{}",orgId, userId);
                xmppService.updateStudentMapFamily(id, orgId, userId,studentId, relationType, flag);
                log.info("xmpp推送学生家长关系完毕.....end，orgId:{},userId:{}",orgId, userId);
            }

            if(userToDoorFlag){
                log.info("进入同步用户信息到door服务.....start，orgId:{},userId:{},roleType:{}",orgId, userId, roleTypeEnum.getValue());
                //暂停1秒等待用户数据提交
                Thread.sleep(1000L);
                doorFeignService.synUserToDoor(orgId, userId, roleTypeEnum.getValue(), isDel);
                log.info("同步用户信息到door服务.....end");
            }
        } catch (InterruptedException e) {
            log.info("同步用户信息到door异常，msg:", e);
            e.printStackTrace();
        }
    }
}
